package liecai_class.day04;

import java.util.Iterator;

public class Entry<K,V> implements Iterable<Entry<K,V>> {
    private K k;
    private V v;
    private int hash;
    private Entry<K,V> next;

    public Entry(K k, V v,Entry<K,V> next) {
        this.k = k;
        this.v = v;
        this.hash = k.hashCode();
        this.next = next;
    }

    public K getK() {
        return k;
    }

    public void setK(K k) {
        this.k = k;
    }

    public V getV() {
        return v;
    }

    public void setV(V v) {
        this.v = v;
    }

    public int getHash() {
        return hash;
    }

    public void setHash(int hash) {
        this.hash = hash;
    }

    public Entry<K, V> getNext() {
        return next;
    }

    public void setNext(Entry<K, V> next) {
        this.next = next;
    }

    @Override
    public String toString() {
        return "Entry{" +
                "k=" + k +
                ", v=" + v +
                ", next=" + next +
                '}';
    }

    @Override
    public Iterator<Entry<K, V>> iterator() {
        return new Iterator<Entry<K, V>>() {
            Entry<K,V> nowEntry = Entry.this;
            @Override
            public boolean hasNext() {
                return nowEntry!=null;
            }

            @Override
            public Entry<K, V> next() {
                try {
                    return nowEntry;
                } finally {
                    nowEntry = nowEntry.getNext();
                }
            }
        };
    }
}
